package cn.edu.xmu.oomall.payment.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.payment.model.vo.*;
import cn.edu.xmu.oomall.payment.service.ShopPaymentService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.ZonedDateTime;

/**
 * 账单信息的controller层
 * @author 张浩山
 * @since 0.1.1-SNAPSHOT
 */
@Api(value = "店铺", tags = "shop")
@RestController
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
@Component
public class ShopPaymentController {
    @Autowired
    private HttpServletResponse httpServletResponse;

    @Autowired
    private ShopPaymentService shopPaymentService;

    /**
     * 平台管理员查询支付信息
     * @param shopId 商铺id
     * @param documentId 单据id
     * @param state 状态
     * @param beginTime 晚于此支付时间
     * @param endTime 早于此支付时间
     * @param page 页码
     * @param pageSize 每页数目
     * @return 支付信息
     */
    @ApiOperation(value = "平台管理员查询支付信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功") ,
            @ApiResponse(code = 505, message = "无权操作")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value ="用户token" ,required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value ="商铺id" ,required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "documentId", value ="单据id" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "state", value ="状态" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value ="晚于此支付时间" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value ="早于此支付时间" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value ="页码" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value ="每页数目" ,required = false),
    })
    @Audit
    @GetMapping(value = "/shops/{shopId}/payment")
    public Object selectPayment(
            @PathVariable Long shopId,
            @RequestParam(required = false)String documentId,
            @RequestParam(required = false)Byte state,
            @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") @RequestParam(required = false) ZonedDateTime beginTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") @RequestParam(required = false) ZonedDateTime endTime,
            @RequestParam(required = false)Integer page,
            @RequestParam(required = false)Integer pageSize
    ) {
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.selectPayment(page, pageSize, documentId, state, beginTime, endTime);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 平台管理员查询支付信息详情
     * @param shopId 店铺id
     * @param id 支付id
     * @return 支付的详细信息
     */
    @ApiOperation(value = "平台管理员查询支付信息详情")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功") ,
            @ApiResponse(code = 505, message = "无权操作")
    })
    //@Audit
    @GetMapping(value = "/shops/{shopId}/payment/{id}")
    public Object selectPaymentDetail(@PathVariable Long shopId, @PathVariable Long id) {
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.selectPaymentDetail(id);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 平台管理员修改支付信息
     * @param paymentVo 修改的信息
     * @param bindingResult 合法性校验
     * @param shopId 店铺id
     * @param id 支付id
     * @param loginUser 登陆用户
     * @param loginUsername 登陆用户名
     * @return 修改的结果
     */
    @ApiOperation(value = "平台管理员修改支付信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功") ,
            @ApiResponse(code = 505, message = "无权操作")
    })
    @Audit
    @PutMapping(value = "/shops/{shopId}/payment/{id}")
    public Object updatePayment(
            @Validated @RequestBody ShopPaymentVo paymentVo,
            BindingResult bindingResult,
            @PathVariable Long shopId,
            @PathVariable Long id,
            @LoginUser Long loginUser,
            @LoginName String loginUsername) {
        Object obj = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (null != obj) {
            return obj;
        }
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.updatePayment(id, paymentVo, loginUser, loginUsername);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 平台管理员查询退款信息
     * @param shopId 商铺id
     * @param documentId 单据id
     * @param state 状态
     * @param beginTime 晚于此支付时间
     * @param endTime 早于此支付时间
     * @param page 页码
     * @param pageSize 每页数目
     * @return 退款信息
     */
    @ApiOperation(value = "平台管理员查询退款信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功") ,
            @ApiResponse(code = 505, message = "无权操作")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value ="用户token" ,required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value ="商铺id" ,required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "documentId", value ="单据id" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "state", value ="状态" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value ="晚于此支付时间" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value ="早于此支付时间" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value ="页码" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value ="每页数目" ,required = false),
    })
    @Audit
    @GetMapping(value = "/shops/{shopId}/refund")
    public Object selectRefund(
            @PathVariable Long shopId,
            @RequestParam(required = false)String documentId,
            @RequestParam(required = false)Byte state,
            @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") @RequestParam(required = false) ZonedDateTime beginTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") @RequestParam(required = false) ZonedDateTime endTime,
            @RequestParam(required = false)Integer page,
            @RequestParam(required = false)Integer pageSize
    ) {
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.selectRefund(page, pageSize, documentId, state, beginTime, endTime);
        return Common.decorateReturnObject(ret);
    }

    /**
     *
     * @param shopId 店铺id
     * @param id 退款id
     * @return 退款信息
     */
    @Audit
    @ApiOperation(value = "平台管理员查询退款信息详情")
    @GetMapping(value = "/shops/{shopId}/refund/{id}")
    public Object selectRefundDetail(@PathVariable Long shopId, @PathVariable Long id) {
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.selectRefundDetail(id);
        return Common.decorateReturnObject(ret);
    }

    @Audit
    @ApiOperation(value = "平台管理员修改退款信息")
    @PutMapping(value = "/shops/{shopId}/refund/{id}")
    public Object updateRefund(
            @Validated @RequestBody ShopRefundVo refundVo,
            BindingResult bindingResult,
            @PathVariable Long shopId,
            @PathVariable Long id,
            @LoginUser Long loginUser,
            @LoginName String loginUsername
    ) {
        Object obj = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (null != obj) {
            return obj;
        }
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.updateRefund(id, refundVo, loginUser, loginUsername);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 平台管理员查询错账信息
     * @param shopId 商铺id
     * @param documentId 单据id
     * @param state 状态
     * @param beginTime 晚于此支付时间
     * @param endTime 早于此支付时间
     * @param page 页码
     * @param pageSize 每页数目
     * @return 错账信息
     */
    @ApiOperation(value = "平台管理员查询错账信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功") ,
            @ApiResponse(code = 505, message = "无权操作")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value ="用户token" ,required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "shopId", value ="商铺id" ,required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "documentId", value ="单据id" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "state", value ="状态" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "beginTime", value ="晚于此支付时间" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endTime", value ="早于此支付时间" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value ="页码" ,required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value ="每页数目" ,required = false),
    })
    @Audit
    @GetMapping(value = "/shops/{shopId}/erroraccounts")
    public Object selectErrorAccount(
            @PathVariable Long shopId,
            @RequestParam(required = false)String documentId,
            @RequestParam(required = false)Byte state,
            @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") @RequestParam(required = false) ZonedDateTime beginTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") @RequestParam(required = false) ZonedDateTime endTime,
            @RequestParam(required = false)Integer page,
            @RequestParam(required = false)Integer pageSize
    ) {
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.selectErrorAccount(page, pageSize, documentId, state, beginTime, endTime);
        return Common.decorateReturnObject(ret);
    }

    @Audit
    @ApiOperation(value = "平台管理员查询错账信息详情")
    @GetMapping(value = "/shops/{shopId}/erroraccounts/{id}")
    public Object selectErrorAccountDetail(@PathVariable Long shopId, @PathVariable Long id) {
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.selectErrorAccountDetail(id);
        return Common.decorateReturnObject(ret);
    }

    @Audit
    @ApiOperation(value = "平台管理员修改错账信息")
    @PutMapping(value = "/shops/{shopId}/erroraccounts/{id}")
    public Object updateErrorAccount(
            @Validated @RequestBody ShopErrorAccountVo errorAccountVo,
            BindingResult bindingResult,
            @PathVariable Long shopId,
            @PathVariable Long id,
            @LoginUser Long loginUser,
            @LoginName String loginUsername
    ) {
        Object obj = Common.processFieldErrors(bindingResult, httpServletResponse);
        if (null != obj) {
            return obj;
        }
        if(shopId != 0){
            return Common.decorateReturnObject(new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE));
        }
        ReturnObject ret = shopPaymentService.updateErrorAccount(id, errorAccountVo, loginUser, loginUsername);
        return Common.decorateReturnObject(ret);
    }
}
